//
//  AccountViewController.swift
//  ZNHXManagement
//
//  Created by 四川中农 on 2018/9/28.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit
import HandyJSON

class AccountViewController: BaseViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var userModel:UserInfoModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.navigationItem.title = "账号信息"
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize.init(width: (UIScreen.ScreenWidth - 15 * 5)/4, height: 33)
        flowLayout.sectionInset = UIEdgeInsetsMake(14, 15, 15, 15)
        flowLayout.scrollDirection = .vertical
        flowLayout.headerReferenceSize = CGSize.init(width: UIScreen.ScreenWidth, height: 320)
        flowLayout.minimumLineSpacing = 15
        flowLayout.minimumInteritemSpacing = 15
        self.collectionView.setCollectionViewLayout(flowLayout, animated: true)
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.register(UINib.init(nibName: "AccountCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "AccountCollectionReusableView")
        
        getMyUserInfo()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension AccountViewController:UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EvaluateCollectionViewCell", for: indexPath) as! EvaluateCollectionViewCell
        cell.tagLabel.text = self.userModel.tags[indexPath.row]
        return cell
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.userModel == nil ? 0 : 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.userModel.tags.count
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "AccountCollectionReusableView", for: indexPath) as! AccountCollectionReusableView
        header.reload(withModel: self.userModel)
        header.statisticsClosure = { [weak self] (value) in
            let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "BusinessDetailsTableViewController") as! BusinessDetailsTableViewController
            ctrl.hidesBottomBarWhenPushed = true
            ctrl.type = value
            self?.navigationController?.pushViewController(ctrl, animated: true)
        }
        return header
    }
    
    
    func getMyUserInfo(){
        PKhud.show()
        HttpRequest.requestData(.post, URLString: Api.userMy.rawValue, parameters: nil) { (result) in
            PKhud.hiden()
            if result["msg"] as! String == "success"{
                if let jsonDic = result["data"] as? postParameters {
                    self.userModel = JSONDeserializer<UserInfoModel>.deserializeFrom(dict: jsonDic)
                    self.collectionView.reloadData()
                }
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }
    
}




